﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Xml.Linq;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework;
using DotNetNuke.Entities.Modules;

namespace HeroldIT.Dnn.Modules.CodeBehind
{
    /// <summary>
    /// Provides a base for the custom Rss feed issuing page.
    /// </summary>
    public class RssFeed : PageBase
    {
        /// <summary>
        /// The module id from which to get the rss feed.
        /// </summary>
        protected int ModuleId { get; set; }

        /// <summary>
        /// The Rss feed's xml document.
        /// </summary>
        protected XDocument RssDoc { get; set; }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.ModuleId = Null.NullInteger;
            string moduleIdValue = this.Request["moduleid"];
            if (!String.IsNullOrEmpty(moduleIdValue))
            {
                int moduleId;
                if (int.TryParse(moduleIdValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out moduleId))
                {
                    this.ModuleId = moduleId;
                }
            }
            if (this.ModuleId == Null.NullInteger)
                this.Bail();

            ModuleController mc = new ModuleController();
            ModuleInfo module = mc.GetModule(this.ModuleId, this.PortalSettings.ActiveTab.TabID, false);
            if (!module.DisplaySyndicate)
                this.Bail();

            ModuleControlInfo control = ModuleControlController.GetModuleControl(module.ModuleControlId);
            Control viewControl = this.LoadControl("~/" + control.ControlSrc);
            PortalModuleBase portalModule = viewControl as PortalModuleBase;
            if (null != portalModule)
            {
                portalModule.ModuleConfiguration = module;
            }
            ISyndicatable syndicatable = viewControl as ISyndicatable;
            if (null == syndicatable)
                this.Fallback(this.ModuleId);
            else
            {
                this.RssDoc = syndicatable.GenerateRssFeed();
                if (null == this.RssDoc)
                {
                    this.Fallback(this.ModuleId);
                }
            }
        }

        /// <summary>
        /// Initializes the <see cref="T:System.Web.UI.HtmlTextWriter"/> object and calls on the child controls of the <see cref="T:System.Web.UI.Page"/> to render.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> that receives the page content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            this.Response.ContentType = "application/atom+xml";
            writer.Write(RssDoc.ToString(SaveOptions.None));
        }

        /// <summary>
        /// Redirects to the portal's home tab, in case an error occurs.
        /// </summary>
        protected void Bail()
        {
            this.Response.Redirect(Globals.NavigateURL(this.PortalSettings.HomeTabId));
        }

        /// <summary>
        /// Redirects to the standard rss feed generator of DNN.
        /// </summary>
        /// <param name="moduleId">The module id for which to get the rss feed.</param>
        protected void Fallback(int moduleId)
        {
            this.Response.Redirect(
                Globals.NavigateURL(this.PortalSettings.ActiveTab.TabID,
                                    "",
                                    new[] {"moduleid=" + moduleId}).Replace("Default.aspx", "RSS.aspx"));
        }
    }
}
